package cn.edu.cqu.bysj.controllers.travelPlanHandle;

import java.util.LinkedList;
import java.util.List;

import cn.edu.cqu.bysj.domains.QueryLeftNewDTO;
import cn.edu.cqu.bysj.domains.TrainOfficeInfo;
import cn.edu.cqu.bysj.services.QueryLeftNewDTOCRUD;
import cn.edu.cqu.bysj.services.TrainOfficeInfoCRUD;

public class AddAllStationHandle implements GetPlanHandle {
	private TrainOfficeInfoCRUD trainOfficeInfoCRUD;
	private QueryLeftNewDTOCRUD queryLeftNewDTOCRUD;
	private GetPlanHandle handler;
	
	public AddAllStationHandle(TrainOfficeInfoCRUD trainOfficeInfoCRUD, QueryLeftNewDTOCRUD queryLeftNewDTOCRUD) {
		super();
		this.trainOfficeInfoCRUD = trainOfficeInfoCRUD;
		this.queryLeftNewDTOCRUD = queryLeftNewDTOCRUD;
	}
	public AddAllStationHandle() {
		super();
	}
	@Override
	public List<QueryLeftNewDTO> handleRequest(String fromStationCode, String toStationCode, String fromStationName,
			String toStationName, String trainTypes, String userStartTime, int index) {
		List<TrainOfficeInfo> trainOfficeInfos1 = new LinkedList<>();
		List<TrainOfficeInfo> trainOfficeInfos2 = new LinkedList<>();
		trainOfficeInfos1 = trainOfficeInfoCRUD.getByOfficeNoLevel(trainOfficeInfoCRUD.getByStationName(fromStationName).getTrainOfficeID(), 2);
		trainOfficeInfos2 = trainOfficeInfoCRUD.getByOfficeNoLevel(trainOfficeInfoCRUD.getByStationName(toStationName).getTrainOfficeID(), 2);
		List<QueryLeftNewDTO>  trains = new LinkedList<>();
		for (TrainOfficeInfo trainOfficeInfo1 : trainOfficeInfos1)
		{
			for (TrainOfficeInfo trainOfficeInfo2 : trainOfficeInfos2)
			{
				List<QueryLeftNewDTO> newTrains3 = queryLeftNewDTOCRUD.getPlan(trainOfficeInfo1.getStationName(), trainOfficeInfo2.getStationName(),trainTypes.equals("")?"G-D-T-Z-K":trainTypes,"");
				if (newTrains3.size() > 0)
				{
					for (QueryLeftNewDTO queryLeftNewDTO3 : newTrains3)
					{
						List<QueryLeftNewDTO> newTrains1 = queryLeftNewDTOCRUD.getPlan(fromStationName, trainOfficeInfo1.getStationName(),trainTypes,userStartTime);
						List<QueryLeftNewDTO> newTrains2 = queryLeftNewDTOCRUD.getPlan(trainOfficeInfo2.getStationName(), toStationName,trainTypes,"");
						if (newTrains1.size() > 0 && newTrains2.size() > 0)
						{
							List<QueryLeftNewDTO> newTrains = new LinkedList<>();
							for (QueryLeftNewDTO queryLeftNewDTO1 : newTrains1)
							{
								for (QueryLeftNewDTO queryLeftNewDTO2 : newTrains2)
								{
									QueryLeftNewDTO queryLeftNewDTOTemp1 = new QueryLeftNewDTO(queryLeftNewDTO1,queryLeftNewDTO3);
									QueryLeftNewDTO queryLeftNewDTOTemp2 = new QueryLeftNewDTO(queryLeftNewDTOTemp1,queryLeftNewDTO2);
									newTrains.add(queryLeftNewDTOTemp2);
								}
							}
							if (newTrains.size() > 0)
							{
								trains.addAll(newTrains);
							}
						}
					}
				}
			}
		}
		if (trains.size() > 0) {
			return trains;
		} else {
			if (handler != null) {
				return handler.handleRequest(fromStationCode,toStationCode,fromStationName, toStationName, trainTypes, userStartTime, index);
			}
			return null;
		}
	}
	@Override
	public void setNextHandler(GetPlanHandle handler) {
		this.handler = handler;
	}

}